/**
 * Created by lvzj on 15/12/5.
 */

var around = "1";

function init() {
    $('#title').html(unescape(params.title));
}

$(window).load(function () {
    //周边小区可见按钮
    var on = true;// 默认开启
    $('.btn-w').on('touchstart', function () {
        if (on) {
            $('.btn-w').addClass('off').removeClass('on');
            on = false;
            around = "0";
        } else {
            $('.btn-w').addClass('on').removeClass('off');
            on = true;
            around = "1";
        }
    });
});


$('#content').keyup(function () {
    if (!trim($('#content').val())) {
        $('.sendbtn').attr('style', 'color: #c0c0c0;');
    } else {
        $('.sendbtn').attr('style', '');
    }
});
$("#content").bind('paste', function () {
    if (!trim($('#content').val())) {
        $('.sendbtn').attr('style', 'color: #c0c0c0;');
    } else {
        $('.sendbtn').attr('style', '');
    }
});

function trim(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
}

var isSend = false;

function send() {
    if (!trim($('#content').val())) return;
    var type = params.type;
    if (!isSend) {

        isSend = true;
        $.ajax({
            type: "POST",
            url: serverUrl + '/rest/v1.0/Questions/newQuestion/' + params.token + '/' + params.deviceId,
            timeout: 5000,
            data: {
                'around': around,
                'desc': '#' + unescape(params.title) + '#' + $('#content').val(),
                'category': 'share',
                'topicId': params.topicId
            },
            dataType: "json",
            success: function (data) {
                isSend = false;
                var status = data.result.status;
                if (status == 1) {
                    if (type == 'ios') {
                        document.location.href = "66xiaoqu:webview:toast##content=发布成功&nbsp;status=1";
                    } else {
                        webviewhandle.showToast("发布成功", 1);
                    }
                } else {
                    if (type == 'ios') {
                        $('#message').value('发布失败');
                        document.location.href = "66xiaoqu:webview:toast##content=" + message + "&nbsp;status=0";
                    } else {
                        webviewhandle.showToast(message, 0);
                    }
                }
                toIndex();
            },
            error: function (xmlHttpRequest, error) {
                isSend = false;
                if (type == 'ios') {
                    $('#message').html('发布失败');
                    document.location.href = "66xiaoqu:webview:toast##content=" + message + "&nbsp;status=0";
                } else {
                    webviewhandle.showToast("发布失败", 0);
                }
            }

        });
    }
}

function toIndex() {
    //window.opener.refresh();
    //window.opener.parent.location.reload()
    //window.opener.location.reload();
    history.back()
    //window.close();
    //location.href = htmlUrl + "/topic/index.html?type="+params.type+"&tid=" + params.topicId + "&token=" + params.token + "&deviceId=" + params.deviceId;
}

function checkContent() {
    if (!trim($('#content').val())) {
        $('.sendbtn').attr('style', 'color: #c0c0c0;');
    } else {
        $('.sendbtn').attr('style', '');
    }
}
setInterval("checkContent()", 1000);
